package server;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class LocalNode {

	private char lotID;
	private int uid;
	private int [] loopVoltage = new int[4];
	private int temp;
	private int lotCount;
	private int lotSize;
	private int sensitivity;
	
	private BufferedImage lotImageGrey;
	private BufferedImage lotImageGreen;
	private BufferedImage lotImageYellow;
	private BufferedImage lotImageRed;
	public int x;
	public int y;
	private Rectangle bounds;
	
	public LocalNode(char lotID, int uid, int[] loopVoltage, int temp) throws IOException{
		String path = new java.io.File(".").getCanonicalPath();
		//System.out.println("file:\\"+path+"\\imgs\\lot2Grey.png");
		URL url = new URL("file:\\"+path+"\\imgs\\lot2Grey.png");
		this.lotImageGrey = ImageIO.read(url); 
		this.x = 81; //will need to fix this at some point TODO
		this.y = 25;
		bounds = new Rectangle(x,y,lotImageGrey.getWidth(), lotImageGrey.getHeight());
		this.lotID = lotID;
		this.uid = uid;
		this.loopVoltage = loopVoltage;
		this.temp = temp;
		loadLotImages();		
	}
	
	public void loadLotImages() throws IOException{
		String path = "file:\\"+new java.io.File(".").getCanonicalPath();
		System.out.println(path);
		System.out.println(new URL(path+"\\imgs\\lot"+lotID+"Green.png"));
		lotImageGreen = ImageIO.read(new URL(path+"\\imgs\\lot"+lotID+"Green.png"));
		lotImageYellow = ImageIO.read(new URL(path+"\\imgs\\lot"+lotID+"Yellow.png"));
		lotImageRed = ImageIO.read(new URL(path+"\\imgs\\lot"+lotID+"Red.png"));
	}	

	public char getLotID() {
		return lotID;
	}

	public void setLotID(char lotID) {
		this.lotID = lotID;
	}

	public int getUID() {
		return uid;
	}

	public void setUID(int uid) {
		this.uid = uid;
	}

	public int getLoopVoltage(int i) {
		if (i < 4 && i >= 0)
			return loopVoltage[i];
		else{
			System.out.println("Bad i value, using 0");
			return loopVoltage[0];
		}
	}

	public void setLoopVoltage(int voltage, int loop) {
		loopVoltage[loop] = voltage;
	}

	public int getTemp() {
		return temp;
	}

	public void setTemp(int temp) {
		this.temp = temp;
	}
	//[B:2:2:1234:5678:1234:5678:77:98:150:70]
	/*(0)cmd
	 * (1)lotid,
	 * (2)uid,
	 * (3)l1,
	 * (4)l2,
	 * (5)l3,
	 * (6)l4,
	 * (7)temp,
	 * (8)lotCount,
	 * (9)lotSize,
	 * (10)sensitivity
	 * Parses the status. If the local node is requesting a status (R command), then it handles it
	 * otherwise, it just updates the 
	 */
	public void parseStatus(Server server, String status) throws InterruptedException {
		String [] Msg = new String[11]; //Denotes the fields above
		status = status.replaceAll("\\[", ""); 
		status = status.replaceAll("\\]", ""); //removes the brackets
		Msg = status.split(":");
		server.logMsg(this.getUID(), "LOT " +this.getLotID(), "SERVER", status);
		if (Msg[0].charAt(0) == 'R'){
			server.sendCmd(Command.SEND_STATUS);
		}
		else {
			lotID = Msg[1].charAt(0);
			uid = Integer.parseInt(Msg[2]);
			for (int i = 3; i <= 6; i++){
				loopVoltage[i-3] = Integer.parseInt(Msg[i]); 
				//puts msg 3,4,5,6 into lv1-4
			}
			temp = Integer.parseInt(Msg[7]);
			setLotCount(Integer.parseInt(Msg[8]));
			setLotSize(Integer.parseInt(Msg[9]));
			setSensitivity(Integer.parseInt(Msg[10]));
		}
	}

	public void incrLotCount(){
		++lotCount;
	}
	
	public void decrLotCout() {
		--lotCount;	
	}
	
	public int getLotCount() {
		return lotCount;
	}

	public void setLotCount(int lotCount) {
		this.lotCount = lotCount;
	}

	public int getLotSize() {
		return lotSize;
	}

	public void setLotSize(int lotSize) {
		this.lotSize = lotSize;
	}

	public int getSensitivity() {
		return sensitivity;
	}

	public void setSensitivity(int sensitivity) {
		this.sensitivity = sensitivity;
	}

	public void incrUID() {
		setUID(getUID()+1); 	
	}

	public Image getLotImage(boolean connected){
		int spaces = lotSize - lotCount;
		if(!connected)
			return lotImageGrey;
		if (spaces <= 1)
			return lotImageRed;
		if (spaces <= 5)
			return lotImageYellow;
		if (spaces > 5)
			return lotImageGreen;
		return lotImageGrey;
	}

	
}
